package com.newcapce.news.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.newcapce.news.dao.UserInfoDao;
import com.newcapce.news.entity.Result;
import com.newcapce.news.entity.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserInfoController {



    @Autowired
    private UserInfoDao userInfoDao;

    @PostMapping("/login.action")
    public Result login(@RequestBody Map<String,String> map, HttpServletRequest request){
        HttpSession session = request.getSession();
        Result result = new Result();
        if(!map.get("verifyCode").equals( (String) request.getSession().getAttribute("googleVerifyCode"))){
            result.setCode(500);
            result.setMsg("验证码错误");
            return result;
        }
        UserInfo userInfo = userInfoDao.login(map.get("userName"),map.get("userPwd"));
        if(userInfo == null){
            result.setCode(500);
            result.setMsg("用户名密码错误");
            return result;
        }

        result.setCode(200);
        result.setMsg("登录成功");
        result.setData(userInfo);
        return result;
    }

    @GetMapping("/selectAll.action")
    public List<UserInfo> selectAll(){
        return userInfoDao.selectAll();
    }

    @GetMapping("/selectByPage.action")
    public Result selectByPage(int limit, int page){
        //启用分页
        PageHelper.startPage(page,limit);
        //正常执行查询
        Page<UserInfo> data = (Page<UserInfo>) userInfoDao.selectAll();
        List<UserInfo> datalist = data.getResult();
        Long count = data.getTotal();
        Map map = new HashMap();
        map.put("total",count);
        map.put("data",datalist);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(map);
        return result;
    }


    @GetMapping("/selectSearchByPage.action")
    //public Result selectSearchByPage(int limit, int page,String userName,String userSex,int userAge){
    public Result selectSearchByPage(int limit, int page,String userName,String userSex,int userAge){
//        int page = (int) parms.get("page");
//        int limit = (int) parms.get("limit");
        System.out.println("------------");
        //启用分页
        PageHelper.startPage(page,limit);
        //正常执行查询
        Page<UserInfo> data = (Page<UserInfo>) userInfoDao.search( userName, userSex, userAge);
        List<UserInfo> datalist = data.getResult();
        Long count = data.getTotal();
        Map map = new HashMap();
        map.put("total",count);
        map.put("data",datalist);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(map);
        return result;
    }


    @PostMapping("/addUser.action")
    public Result addUser(@RequestBody UserInfo userInfo){
        int userId = userInfoDao.insertSelective(userInfo);

        Result result = new Result();
        result.setCode(200);
        result.setMsg("添加成功");

        return result;
    }


    @PutMapping("/updateUser.action")
    public Result updateUser(@RequestBody UserInfo userInfo){
        int userId = userInfoDao.updateByPrimaryKeySelective(userInfo);

        Result result = new Result();
        result.setCode(200);
        result.setMsg("更新成功");

        return result;
    }


    @DeleteMapping("/deleteById.action/{userId}")
    public Result addUser(@PathVariable("userId") int userId){
        boolean flag = userInfoDao.deleteByPrimaryKey(userId);

        Result result = new Result();
        if(flag) {
            result.setCode(200);
            result.setMsg("删除成功");
        }else{
            result.setCode(500);
            result.setMsg("删除失败");
        }

        return result;
    }


}
